/*
 * GuiLogin.java
 *
 * Created on 12 April 2003, 08:19
 * Copyright (C) 2003, Velocityme Partnership
 */

package com.velocityme.client.gui;

import com.velocityme.client.gui.exception.ExceptionWindow;
import com.velocityme.interfaces.KeySession;
import com.velocityme.interfaces.KeySessionUtil;
import com.velocityme.session.KeySessionBean;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.naming.NamingException;

/**
 *
 * @author  Robert Crida Work
 */
public class GuiLogin extends javax.swing.JDialog {
    
    private KeySession m_key;
    
    /** Creates new form GuiLogin */
    public GuiLogin(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    public KeySession getKey() {
        return m_key;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        jLabelUsername = new javax.swing.JLabel();
        jTextFieldUsername = new javax.swing.JTextField();
        jLabelPassword = new javax.swing.JLabel();
        jPasswordField = new javax.swing.JPasswordField();
        jLabelMessage = new javax.swing.JLabel();
        jPanelButtons = new javax.swing.JPanel();
        jButtonLogin = new javax.swing.JButton();
        jButtonQuit = new javax.swing.JButton();

        getContentPane().setLayout(new java.awt.GridBagLayout());

        setTitle("Velocityme Client Login");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jLabelUsername.setText("Username:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        getContentPane().add(jLabelUsername, gridBagConstraints);

        jTextFieldUsername.setPreferredSize(new java.awt.Dimension(100, 20));
        jTextFieldUsername.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldUsernameActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        getContentPane().add(jTextFieldUsername, gridBagConstraints);

        jLabelPassword.setText("Password:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        getContentPane().add(jLabelPassword, gridBagConstraints);

        jPasswordField.setNextFocusableComponent(jButtonLogin);
        jPasswordField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPasswordFieldActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        getContentPane().add(jPasswordField, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        getContentPane().add(jLabelMessage, gridBagConstraints);

        jPanelButtons.setLayout(new java.awt.GridLayout(1, 0));

        jButtonLogin.setText("Login");
        jButtonLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLoginActionPerformed(evt);
            }
        });

        jPanelButtons.add(jButtonLogin);

        jButtonQuit.setText("Quit");
        jButtonQuit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonQuitActionPerformed(evt);
            }
        });

        jPanelButtons.add(jButtonQuit);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        getContentPane().add(jPanelButtons, gridBagConstraints);

        pack();
    }//GEN-END:initComponents

    private void jButtonQuitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonQuitActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_jButtonQuitActionPerformed

    private void jTextFieldUsernameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldUsernameActionPerformed
        // Add your handling code here:
        loginActionPerformed();        
    }//GEN-LAST:event_jTextFieldUsernameActionPerformed
    
    private void jPasswordFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPasswordFieldActionPerformed
        // Add your handling code here:
        loginActionPerformed();
    }//GEN-LAST:event_jPasswordFieldActionPerformed

    private void jButtonLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLoginActionPerformed
        // Add your handling code here:
        loginActionPerformed();
    }//GEN-LAST:event_jButtonLoginActionPerformed
    
    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        System.exit(0);
    }//GEN-LAST:event_closeDialog
    
    private void loginActionPerformed() {
        try {
            if (startSession()) {
                switch (m_key.login(jTextFieldUsername.getText(), jPasswordField.getText(), getClass().getPackage().getImplementationVersion(),"Swing Client")) {
                    case KeySessionBean.LOGIN_SUCCESS: setVisible(false); dispose(); break;
                    case KeySessionBean.LOGIN_LICENCE: jLabelMessage.setText("No available licence"); pack(); break;
                    case KeySessionBean.LOGIN_PASSWORD: jLabelMessage.setText("Incorrect password"); pack(); break;
                    case KeySessionBean.LOGIN_INVALID: jLabelMessage.setText("Invalid username"); pack(); break;
                    default:
                    case KeySessionBean.LOGIN_ERROR: jLabelMessage.setText("Server error"); pack(); break;                    
                }
            }
        }
        catch(Exception e) {
            ExceptionWindow.showError(e);
        }
    }

    private boolean startSession(){
        try {
            m_key = KeySessionUtil.getHome().create();
            return true;
        }
        catch(NamingException e) {
            jLabelMessage.setText("NamingException: " + e.getMessage());
        }
        catch(CreateException e) {
            jLabelMessage.setText("CreateException: " + e.getMessage());
        }
        catch(RemoteException e) {
            jLabelMessage.setText("RemoteException: " + e.getMessage());
        }
        finally {
            pack();
        }
        return false;
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        new GuiLogin(new javax.swing.JFrame(), true).show();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonLogin;
    private javax.swing.JButton jButtonQuit;
    private javax.swing.JLabel jLabelMessage;
    private javax.swing.JLabel jLabelPassword;
    private javax.swing.JLabel jLabelUsername;
    private javax.swing.JPanel jPanelButtons;
    private javax.swing.JPasswordField jPasswordField;
    private javax.swing.JTextField jTextFieldUsername;
    // End of variables declaration//GEN-END:variables
    
}
